package org.javaforever.cookieshop.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.javaforever.cookieshop.domain.UserBean;
import org.javaforever.cookieshop.service.UserService;
import org.javaforever.cookieshop.shiro.UserRegisteAndLogin;
import org.javaforever.cookieshop.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@RequestMapping(value = "/profileController")
@RestController
public class ProfileController {
	public UserBean tempUser = new UserBean();
	@Autowired
	protected UserService service;

	@RequestMapping(value = "/addUploadUserPhoto", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addUploadUserPhoto(MultipartHttpServletRequest request) throws Exception{
        Map<String, Object> result = new TreeMap<String, Object>();
        Iterator iter = request.getFileNames();
        byte[] imageBytes = null;

        while (iter.hasNext()) {
            MultipartFile file = request.getFile(iter.next().toString());

            if (file != null) {
                imageBytes = file.getBytes();
                if (tempUser == null) tempUser = new UserBean();
                tempUser.setPhoto(imageBytes);
                break;
            }
        }
        if (imageBytes != null) {
            result.put("success", true);
            result.put("data", imageBytes);
        } else {
            result.put("success", false);
            result.put("data", null);
        }
        return result;
	}
	@RequestMapping(value = "/changeMyPasswordUser", method = RequestMethod.POST)
	public Map<String,Object> changeMyPasswordUser(String oldPassword,String password) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        Session session  = SecurityUtils.getSubject().getSession();
        UserBean user0 = (UserBean)session.getAttribute("user");
        if(user0!=null&&!StringUtil.isBlank(user0.getUserName())) {
        String oldSalt = service.getSaltUser(user0.getUserName());
        String verfyPassword = UserRegisteAndLogin.getInputPasswordCiph(oldPassword,oldSalt);
        String dboldPassword = service.getPasswordUser(user0.getUserName());
        if (verfyPassword.equals(dboldPassword)) {
            UserBean user = service.findUserByUserName(user0.getUserName());

            String[] saltAndCiphertext = UserRegisteAndLogin.encryptPassword(password);
            user.setLoginFailure(0);
            user.setSalt(saltAndCiphertext[0]);
            user.setPassword(saltAndCiphertext[1]);

            service.changePasswordUser(user);
            result.put("success",true);
            result.put("data",null);

            return result;
            }
        }
        result.put("success",false);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/findMyProfileUser", method = RequestMethod.POST)
	public Map<String,Object> findMyProfileUser() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
                Session session  = SecurityUtils.getSubject().getSession();
        UserBean user = (UserBean)session.getAttribute("user");
        if(user!=null&&!StringUtil.isBlank(user.getUserName())) {
            user = service.findUserByUserName(user.getUserName());
            result.put("success",true);
            result.put("rows",user);
            return result;
        }
        result.put("success",false);
        result.put("rows",null);
        return result;
	}
	@RequestMapping(value = "/updateMyProfileUser", method = RequestMethod.POST)
	public Map<String,Object> updateMyProfileUser(@RequestBody UserBean user) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        Session session  = SecurityUtils.getSubject().getSession();
        UserBean oriUser = (UserBean)session.getAttribute("user");
        if(oriUser!=null&&!StringUtil.isBlank(oriUser.getUserName())) {
            oriUser = service.findUserByUserName(oriUser.getUserName());
            user.setId(oriUser.getId());
            if (oriUser.getPhoto()!=null) {
                user.setPhoto(oriUser.getPhoto());
            }
            if (tempUser!=null && tempUser.getPhoto()!=null) {
                user.setPhoto(tempUser.getPhoto());
            }
            tempUser = new  UserBean();
            tempUser = new  UserBean();
            service.updateUser(user);
            result.put("success",true);
            result.put("data",null);
            return result;
        }
        result.put("success",false);
        result.put("data",null);
        return result;
	}
}

